<?php

namespace app\api\controller;

use app\admin\model\Appfuns;
use app\admin\model\Twocate;
use Throwable;
use think\Response;
use app\common\library\Upload;
use app\common\controller\Frontend;

class Ajax extends Frontend
{
    protected array $noNeedPermission = ["*"];
    protected array $noNeedLogin = ['area', 'buildSuffixSvg', 'towcate'];

    public function initialize(): void
    {
        parent::initialize();
    }

    public function upload(): void
    {
        $file = $this->request->file('file');
        $driver = $this->request->param('driver', 'local');
        $topic = $this->request->param('topic', 'default');
        try {
            $upload = new Upload();
            $attachment = $upload
                ->setFile($file)
                ->setDriver($driver)
                ->setTopic($topic)
                ->upload(null, 0, $this->auth->id);
            unset($attachment['create_time'], $attachment['quote']);
        } catch (Throwable $e) {
            $this->error($e->getMessage());
        }

        $this->success(__('File uploaded successfully'), [
            'file' => $attachment ?? []
        ]);
    }

    /**
     * 省份地区数据
     * @throws Throwable
     */
    public function area(): void
    {
        $this->success('', get_area());
    }

    public function buildSuffixSvg(): Response
    {
        $suffix = $this->request->param('suffix', 'file');
        $background = $this->request->param('background');
        $content = build_suffix_svg((string)$suffix, (string)$background);
        return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
    }

    public function towcate()
    {
        $twocate = (new Appfuns())
            ->where([
                ['status', '=', 1],
            ])
            ->whereNotIn('id', [3, 4])
            ->order('weigh', 'desc')
            ->column('id,name');
        $this->success('', $twocate);
    }
}